using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Threading;

namespace DrawerMenu;

public partial class SplashWindow : Window
{
    private readonly DispatcherTimer _loadingTimer;
    private int _loadingStep = 0;
    private readonly string[] _loadingMessages = new[]
    {
        "正在初始化...",
        "正在加载服务...",
        "正在配置导航...",
        "正在准备页面...",
        "即将完成..."
    };

    public SplashWindow()
    {
        InitializeComponent();
        StartLoadingAnimation();
        
        _loadingTimer = new DispatcherTimer
        {
            Interval = TimeSpan.FromMilliseconds(600)
        };
        _loadingTimer.Tick += LoadingTimer_Tick;
        _loadingTimer.Start();
    }

    private void StartLoadingAnimation()
    {
        var rotationAnimation = new DoubleAnimation
        {
            From = 0,
            To = 360,
            Duration = TimeSpan.FromSeconds(1),
            RepeatBehavior = RepeatBehavior.Forever
        };
        
        LoadingRotation.BeginAnimation(RotateTransform.AngleProperty, rotationAnimation);
    }

    private void LoadingTimer_Tick(object? sender, EventArgs e)
    {
        if (_loadingStep < _loadingMessages.Length)
        {
            LoadingText.Text = _loadingMessages[_loadingStep];
            _loadingStep++;
        }
        else
        {
            _loadingTimer.Stop();
            OnLoadingComplete();
        }
    }

    public event EventHandler? LoadingComplete;

    private void OnLoadingComplete()
    {
        LoadingComplete?.Invoke(this, EventArgs.Empty);
    }

    public void SetProgress(string message)
    {
        Dispatcher.Invoke(() =>
        {
            LoadingText.Text = message;
        });
    }
}
